MPT.addAction("login_form", function(_elm) {

    var form_jQ = $(_elm);
    var prompt_jQ = $('.e_prompt', _elm);
    var account_jQ = $('.e_account', _elm);
    var password_jQ = $('.e_password', _elm);
    var auth_input_jQ = $('.e_auth_input', _elm);
    var auth_img_jQ = $('.e_auth_img', _elm);
    var auth_change_jQ = $('.e_auth_change', _elm);
    var auth_area_jQ = $('.e_auth_area', _elm);
    var submit_jQ = $('.e_submit', _elm);

    var prompt_map = { 'pwd': {}, 'account': {}, 'randCode': {} };
    prompt_map['pwd'][-2] = "请输入密码";
    prompt_map['pwd'][-9] = "密码错误";
    prompt_map['account'][-1] = "账号是6-16位小写字母或数字组成";
    prompt_map['account'][-2] = "请输入账号";
    prompt_map['account'][-10] = "账号已列入黑名单";
    prompt_map['randCode'][-3] = "验证码错误";

    var preValidate = function() {
        prompt_jQ.hide();

        var validate = false;

        var prompt = '';

        if (Verifier.empty(account_jQ.val())) {

            prompt = prompt_map['account'][-2];

        } else if (Verifier.empty(password_jQ.val())) {

            prompt = prompt_map['pwd'][-2];

        } else if (!Verifier.account(account_jQ.val())) {

            prompt = prompt_map['account'][-1];

        } else if (auth_area_jQ.css('display') == 'block' && !Verifier.authcode(auth_input_jQ.val())) {

            prompt = prompt_map['randCode'][-3];

        } else {

            prompt = '';
            validate = true;
        }

        prompt_jQ.text(prompt);
        return validate;
    };

    var doLogin = function() {

        if (!preValidate()) {
            prompt_jQ.show();
        } else {
            jQuery.ajax({
                type: "POST",
                url: "/auth/login",
                data: { 'account': account_jQ.val(), 'pwd': password_jQ.val(), 'randCode': auth_input_jQ.val() },
                dataType: 'json',
                success: function(_response) {
                    if (_response['result'] == 1) {
                        // 选择了记住账号
                        if ($(".e_remember", _elm).attr('checked') == "checked") {
                            var str = BASE64.encode(account_jQ.val());
                            jQuery.cookie("my_account", str, { expires: 7 }); // 存储一个带7天期限cookie
                        }

                        // 没有昵称弹出设置昵称
                        if (!_response['data']['nickname']) {
                            Dialog.show('setNickname', {
                                "child_html": MPT.getTmpl('nickname_dialog'),
                                "width": 440,
                                "height": 250
                            });

                            SetNickname.setElm($('.nickname_dialog')); // 设置修改昵称校验码
                            SetNickname.setPrivacyCode(_response['data']['privacyCode']);

                            return;
                        }
                        window.location.href = MPT.Config['url']['myspace'];
                    }

                    if (_response['result'] == -99) {
                        for (var key in _response['data']) {
                            if ((key == 'randCode' || key == 'pwd' || key == 'account') && _response['data'][key] != 1) {
                                prompt_jQ.text(prompt_map[key][_response['data'][key]]).show();
                            }
                        }

                        if (_response['data']['loginErrorTimes'] > 3) {
                            auth_area_jQ.show();
                            initAuthcode();
                        }
                    }
                }
            });
        }
    };

    var initAuthcode = function() {
        auth_img_jQ.attr('src', "/authimg?type=login&t=" + Math.random());

        auth_change_jQ.click(function() {
            auth_img_jQ.attr('src', "/authimg?type=login&t=" + Math.random());
        });

        Util.KeyBoardMonitor(auth_input_jQ, 13, function() { doLogin(); });
    };

    if (auth_area_jQ.css('display') == 'block') {
        initAuthcode();
    }

    jQuery.getScript(MPT.Config['sta_js_lib_http'] + '/jquery/external/jquery.cookie.js', function() {

        var my_account = jQuery.cookie("my_account");
        if (my_account) {
            my_account = BASE64.decode(my_account);
            account_jQ.val(my_account);
        }

        submit_jQ.click(function() { doLogin(); });
        Util.KeyBoardMonitor(account_jQ, 13, function() { doLogin(); });
        Util.KeyBoardMonitor(password_jQ, 13, function() { doLogin(); });
    });

});
